package jianzhioffer;

import java.util.LinkedList;
import java.util.Queue;

// 剑指 Offer 27. 二叉树的镜像
public class MirrorTree {


    public TreeNode mirrorTree(TreeNode root) {
        if (root==null){
            return null;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while (!queue.isEmpty()){
            TreeNode node = queue.poll();
            if (node.left!=null){

                queue.offer(node.left);
            }
            if (node.right!=null){
                queue.offer(node.right);
            }
            TreeNode t = node.left;
            node.left = node.right;
            node.right = t;
        }
        return root;
    }


    // 递归
    public TreeNode mirrorTree2(TreeNode root) {
        if (root==null){
            return null;
        }
        TreeNode t =  mirrorTree2(root.left);
        root.left = mirrorTree2(root.right);
        root.right = t;
        return root;

    }
}
